/*
 * 
 * 
 * @author: Oren Zamir
 */
package org.valire.usermanagement.services;

import java.util.List;
import java.util.Set;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.valire.usermanagement.NodeAccessPermissionManagementFacade;
import org.valire.usermanagement.beans.NodeAccessPermission;
import org.valire.usermanagement.beans.Role;
import org.valire.usermanagement.beans.User;
import org.valire.util.lang.Args;


/**
 * The Class AccessControlMethodServiceImpl.
 */
public class NodeAccessPermissionServiceImpl implements NodeAccessPermissionService {

	/** The Constant logger. */
	@SuppressWarnings("unused")
	private final static Log logger = LogFactory.getLog(NodeAccessPermissionServiceImpl.class);

	/** The roles dao. */
	@Resource
	private NodeAccessPermissionDao napDao;

	public void refreshCache(){
		napDao.cacheAllNAPs();
	}
	/**
	 * Sets the nap dao.
	 * 
	 * @param napDao
	 *            the new nap dao
	 */
	public void setNapDao(NodeAccessPermissionDao napDao) {
		this.napDao = napDao;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.valire.usermanagement.services.NodeAccessPermissionService#createNAP(org.valire.usermanagement.beans.NodeAccessPermission)
	 */
	@Override
	public NodeAccessPermission createNAP(NodeAccessPermission nap) {
		Args.assertNotNull(nap);
		this.napDao.create(nap);
		return nap;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.valire.usermanagement.services.NodeAccessPermissionService#removeNAP(org.valire.usermanagement.beans.NodeAccessPermission)
	 */
	@Override
	public void removeNAP(NodeAccessPermission nap) {
		Args.assertNotNull(nap);
		this.napDao.setPersistentClass(NodeAccessPermission.class);
		this.napDao.delete(nap.getId());
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.valire.usermanagement.services.NodeAccessPermissionService#getAllNAPs()
	 */
	@Override
	public List<NodeAccessPermission> getAllNAPs() {
		return this.napDao.getAllNAPs();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.valire.usermanagement.services.NodeAccessPermissionService#getNAPsForRoles(java.util.Set)
	 */
	@Override
	public Set<NodeAccessPermission> getNAPsForRoles(Set<Role> roles) {
		return this.napDao.getNAPSForRoles(roles);
	}
	
	public Set<NodeAccessPermission> getNAPsForRole(Role role) {
		return this.napDao.getNAPSForRole(role);
	}
	
	/*
	 * (non-Javadoc)
	 * 
	 * @see org.valire.usermanagement.services.NodeAccessPermissionService#hasPermission(org.valire.usermanagement.beans.Role, java.lang.String)
	 */
	@Override
	public boolean hasPermissionForRole(Role role, String method,  NodeAccessPermissionManagementFacade.NAPValidationInterface validationInterface) {
		return this.napDao.canRoleAccessNode(role, method, validationInterface);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.valire.usermanagement.services.NodeAccessPermissionService#hasPermissionForRoles(java.util.Set, java.lang.String)
	 */
	@Override
	public boolean hasPermissionForRoles(Set<Role> roles, String method,  NodeAccessPermissionManagementFacade.NAPValidationInterface validationInterface) {
		return this.napDao.canRolesAccessNode(roles, method, validationInterface);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.valire.usermanagement.services.NodeAccessPermissionService#updateNAPs(org.valire.usermanagement.beans.NodeAccessPermission)
	 */
	@Override
	public void updateNAPs(NodeAccessPermission nap) {
		Args.assertNotNull(nap.getId());
		Args.assertNotNull(nap.getNodeReference());
		napDao.merge(nap);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.valire.usermanagement.services.NodeAccessPermissionService#getNAPsForUsers(java.util.Set)
	 */
	@Override
	public Set<NodeAccessPermission> getNAPsForUsers(Set<User> users) {
		return this.napDao.getNAPSForUsers(users);
	}

	/* (non-Javadoc)
	 * @see org.valire.usermanagement.services.NodeAccessPermissionService#getNAPsForUser(org.valire.usermanagement.beans.User)
	 */
	@Override
	public Set<NodeAccessPermission> getNAPsForUser(User user) {
		return this.napDao.getNAPSForUser(user);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.valire.usermanagement.services.NodeAccessPermissionService#hasPermissionForUser(org.valire.usermanagement.beans.User, java.lang.String)
	 */
	@Override
	public boolean hasPermissionForUser(User user, String method, NodeAccessPermissionManagementFacade.NAPValidationInterface validationInterface  ) {
		return this.napDao.canUserAccessNode(user, method, validationInterface);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.valire.usermanagement.services.NodeAccessPermissionService#hasPermissionForUsers(java.util.Set, java.lang.String)
	 */
	@Override
	public boolean hasPermissionForUsers(Set<User> users, String method,  NodeAccessPermissionManagementFacade.NAPValidationInterface validationInterface) {
		return this.napDao.canUsersAccessNode(users, method, validationInterface);
	}

	/* (non-Javadoc)
	 * @see org.valire.usermanagement.services.NodeAccessPermissionService#removeAllNAPsForUsers(java.util.Set)
	 */
	@Override
	public void removeAllNAPsForUsers(Set<User> users) {
		this.napDao.removeAllNapsForUsers(users);
	}
	
	/* (non-Javadoc)
	 * @see org.valire.usermanagement.services.NodeAccessPermissionService#removeAllNAPsForRoles(java.util.Set)
	 */
	@Override
	public void removeAllNAPsForRoles(Set<Role> roles) {
		this.napDao.removeAllNapsForRoles(roles);
	}

}
